﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BAL.EstRec;
using System.ComponentModel;

namespace Nav3
{
    /// <summary>
    /// Lógica de interacción para DlgElegirRecurso.xaml
    /// </summary>
    public partial class DlgElegirRecurso : Window
    {
        public ObservableCollection<RecursoWithCheck> recursos;
        public bool isOk;

        public DlgElegirRecurso()
        {
            InitializeComponent();

            this.isOk = false;
            this.recursos = new ObservableCollection<RecursoWithCheck>();
            //this.loadRecurso();
            this.listView.ItemsSource = this.recursos;
        }

        public void loadRecurso()
        {
            try
            {
                List<Recurso> recursos = Recurso.GetRecursosList();
                var res = from o in recursos
                          orderby o.idRecurso
                          select o;
                foreach (Recurso r in res)
                {
                    this.recursos.Add(new RecursoWithCheck(r));
                }
            }
            catch (Exception ex)
            {
                
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.isOk = true;
            this.Close();
        }

        public List<Recurso> getSelectedRecursos()
        {
            List<Recurso> r=new List<Recurso>();
            foreach (RecursoWithCheck rc in this.recursos)
            {
                if(rc.IsChecked)
                    r.Add(new Recurso(rc.idRecurso, rc.recurso));
            }
            return r;
        }

        private void Window_Closed(object sender, System.EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }

    public class RecursoWithCheck : Recurso,INotifyPropertyChanged
    {
        private bool _isChecked;
        public bool IsChecked {
            get { return _isChecked; } 
            set {
                if (value != _isChecked)
                {
                    this._isChecked = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Pos"));
                    }
                }
            }//set
        }

        public RecursoWithCheck(Recurso r)
        {
            this._isChecked = false;
            this.idRecurso = r.idRecurso;
            this.recurso = r.recurso;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
